package com.metadata_system.common.db.mapper;

import com.metadata_system.common.db.entity.Project;
import com.metadata_system.system.response.NodePropertiesItem;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;


public interface ProjectMapper extends Mapper<Project> {
    @Select("select last_insert_id()")
    Integer getLastInsertId();

//    @Select("select * from md_project_node_data_${projectId}_${nodeId} LIMIT #{offset},#{limit}")
    List<Map<String, Object>> getNodeDataList(@Param("projectId") Integer projectId,
                                              @Param("nodeId") Integer nodeId,
                                              @Param("offset") Integer offset,
                                              @Param("limit") Integer limit,
                                              @Param("conditionList") List<String> conditionList,
                                              @Param("order") String order);

//    @Select("select count(*) from md_project_node_data_${projectId}_${nodeId}")
    int getNodeDataTotal(@Param("projectId") Integer projectId,
                             @Param("nodeId") Integer nodeId,
                             @Param("conditionList") List<String> conditionList);

    // 检查表是否存在
    @Select("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = #{tableName}")
    int checkTableExists(@Param("tableName") String tableName);

    // 创建数据表
    void createTable(@Param("tableName") String tableName, @Param("nodePropertiesItems") List<NodePropertiesItem> nodePropertiesItems);

    // 批量插入数据
    void insertAllData(@Param("tableName") String tableName, @Param("fieldList") List<String> fieldList, @Param("dataList") List<Map<String, Object>> dataList);

    void deleteAllData(@Param("tableName") String tableName, @Param("idList") List<Integer> idList);

    // 批量删除数据
    void deleteAllDataByProjectLogId(@Param("tableName") String tableName, @Param("projectLogId") Integer projectLogId);

    @Select("select project_log_id from ${tableName} where projectLogId=${idList}")
    List<Integer> getProjectLogIdList(@Param("tableName") String tableName, @Param("projectLogId") Integer projectLogId);

    @Select("select ifnull(sum(data_total),0) from md_project where database_id = #{databaseId}")
    Integer getDatabaseDataTotal(@Param("databaseId") Integer databaseId);
}